#include "Object.h"

//============================================================================================== OBJECT

int Object::sID = 0;

Object::Object()
{
	Object::sID += 1;
	m_uID = Object::sID;
	m_name = "Default";
	m_Components.reserve(500);	// Reserve memory
}

Object::Object(string name)
{
	Object::sID += 1;
	m_uID = Object::sID;
	m_name = name;
	m_Components.reserve(500);	// Reserve memory
}



// Manage componet will not store any duplicated components
void Object::ManageComponent(Component* comp)
{
	//for(it = m_Components.begin(); it != m_Components.end(); ++it)
	//{
	//	if( ((*it)->getFamilyType() == comp->getFamilyType()) && 
	//		((*it)->getComponentType() == comp->getComponentType()) )
	//	{
	//		// Do nothing
	//	}
	//	else
	//	{
	//		// Add component to object's vector store
	//		m_Components.push_back(comp);
	//		comp->setObjectOwner(this);
	//	}
	//}

	if(!isComponentInStore(comp))
	{
		m_Components.push_back(comp);
		comp->setObjectOwner(this);
		comp->setup();
	}
}

Component* Object::getComponent(std::string familyType, std::string componentType)
{
	for(it = m_Components.begin(); it != m_Components.end(); ++it)
	{
		if( ((*it)->getFamilyType() == familyType) && 
			((*it)->getComponentType() == componentType) )
		{
			return (*it);
		}
		else
		{
			return NULL;
		}
	}

	return NULL;
}

bool Object::isComponentInStore(Component* comp)
{
	for(it = m_Components.begin(); it != m_Components.end(); ++it)
	{
		if( ((*it)->getFamilyType() == comp->getFamilyType()) && 
			((*it)->getComponentType() == comp->getComponentType()) )
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	return false;
}

void Object::clearComponents()
{
	if(!m_Components.empty())
		m_Components.clear();
}

void Object::updateComponents()
{
	for(it = m_Components.begin(); it != m_Components.end(); ++it)
	{
		if((*it) != NULL)
			(*it)->update();
	}
}


//============================================================================================= OBJECT MANAGER


ObjectManager::ObjectManager()
{
	m_objects.reserve(500);		// Reserve memory for objects. Approximation for the amount of objects in a scene
}

ObjectManager::~ObjectManager()
{
}

void ObjectManager::manageObject(Object *obj)
{
	if(obj != NULL)
	{
		m_objects.push_back(obj);
	}
}

Object* ObjectManager::getObject(int id)
{
	for(it = m_objects.begin(); it != m_objects.end(); ++it)
	{
		if((*it)->getID() == id)
			return (*it);
		else
			return NULL;

	}

	return NULL;
}

bool ObjectManager::isObjectInStore(Object *obj)
{
	for(it = m_objects.begin(); it != m_objects.end(); ++it)
	{
		if((*it) == obj)
			return true;
	}
	return false;
}

void ObjectManager::clearObjects(void)
{
	if(!m_objects.empty())
		m_objects.clear();
}


void ObjectManager::updateObjects(void)
{
	if(!m_objects.empty())
	{
		// Update objects here
		for(it = m_objects.begin(); it != m_objects.end(); ++it)
		{
			(*it)->objectUpdate();
		}
	}
}
